package com.china08.yxyapi.service.tifenbao.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.china08.yxyapi.api.manage.stu.mystudy.tifenbao.scoreimprove.TifenbaoKnowLstlController.CharpterModel;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlanKnow;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanKnowRepository;
import com.china08.yxyapi.service.tifenbao.TifenbaoKnowLstService;

@Service
public class TifenbaoKnowLstServiceImpl implements TifenbaoKnowLstService {
    @Autowired
    private YxyHuixuePlanKnowRepository yxyHuixuePlanKnowRepository;
    List<String> list1 = new ArrayList<String>();

    @Override
    @Transactional
    public List<Object[]> list(String planId, String studentId) {
        List<Object[]> objs = new ArrayList<Object[]>();// 学习计划中章节下面所对应的所有小节及知识点情况
        if (StringUtils.isNotBlank(planId)) {
            // 通过计划id查出来总共有多少个知识点
            List<YxyHuixuePlanKnow> knows = yxyHuixuePlanKnowRepository.findByPlanIdOrderByOrderNumAsc(planId);
            if (knows != null && knows.size() > 0) {
                // 根据章节名字对知识点集合进行分组
                Map<String, List<YxyHuixuePlanKnow>> groupByYxyHuixuePlanKnowTrain = knows.stream()
                        .collect(Collectors.groupingBy(YxyHuixuePlanKnow::getBookContentsName));
                // 根据orderNum对分组后的集合进行排序
                Map<String, List<YxyHuixuePlanKnow>> mapknow = new LinkedHashMap<>();
                groupByYxyHuixuePlanKnowTrain.entrySet().stream()
                        .sorted(new Comparator<Map.Entry<String, List<YxyHuixuePlanKnow>>>() {
                            @Override
                            public int compare(Entry<String, List<YxyHuixuePlanKnow>> o1,
                                    Entry<String, List<YxyHuixuePlanKnow>> o2) {
                                return o1.getValue().get(0).getOrderNum().compareTo(o2.getValue().get(0).getOrderNum()); // 升序
                            }
                        }).forEachOrdered(e -> mapknow.put(e.getKey(), e.getValue()));

                Object[] objects2 = null;
                List<CharpterModel> charpterModels = null;
                for (String key : mapknow.keySet()) {
                    CharpterModel charpterModel = null;
                    charpterModels = new ArrayList<CharpterModel>();
                    objects2 = new Object[2];
                    objects2[0] = key;
                    for (YxyHuixuePlanKnow yxyHuixuePlanKnow : mapknow.get(key)) {
                        charpterModel = new CharpterModel();
                        charpterModel.id = yxyHuixuePlanKnow.getId();
                        charpterModel.knowName = yxyHuixuePlanKnow.getKnowName();
                        charpterModel.attention = yxyHuixuePlanKnow.getAttention();
                        charpterModel.pkid = yxyHuixuePlanKnow.getPkId();
                        charpterModel.knowId = yxyHuixuePlanKnow.getKnowId();
                        charpterModel.mubiao = yxyHuixuePlanKnow.getTask();
                        charpterModel.wancheng = yxyHuixuePlanKnow.getPassTask();
                        charpterModels.add(charpterModel);
                    }
                    objects2[1] = charpterModels;
                    objs.add(objects2);
                }
            }

        }
        return objs;
    }

}
